<?php
/**
 * 现场抽奖
 *
 * @author 南风工作室
 * @url http://bbs.we7.cc/
 */
defined('IN_IA') or exit('Access Denied');

class Southwind_livedrawModuleSite extends WeModuleSite {
	
	
	public function doWebActivity() {
		//这个操作被定义用来呈现 功能封面
		global $_W,$_GPC;
		$uniacid =$_W['uniacid'];
		$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		load()->func('tpl');
	    load()->func('file');
		if('post' == $op){//添加或修改
			$id = intval($_GPC['id']);
			$item_id = intval($_GPC['item_id']);
			if(!empty($id)){
				$item = pdo_fetch("SELECT * FROM ".tablename('luck_draw')." where uniacid=:uniacid AND id=:id",array(':uniacid'=>$uniacid, ':id'=>$id));
				empty($item)?message('亲,数据不存在！', '', 'error'):"";
			}
			$step = intval($_GPC['step']) ? intval($_GPC['step']) : 1;
			if ($step == 1) {
					
			}elseif($step == 2) {
				$items = pdo_fetchall("SELECT * FROM " . tablename('luck_draw_item') . " WHERE uniacid = '{$uniacid}' AND draw_id = '{$id}' ORDER BY id ASC");
				if ($item_id) {
					$item_info = pdo_fetch("SELECT * FROM " . tablename('luck_draw_item') . " WHERE id = :id", array(':id' => $item_id));
				}
				if(checksubmit('submit')){
					empty ($_GPC['title'])?message('亲,标题不能为空'):$title=$_GPC['title'];
					$logo =$_GPC['logo'];
					$backdrop =$_GPC['backdrop'];
					$originator =$_GPC['originator'];
					empty ($_GPC['num'])?message('亲,抽奖不能为空'):$num=$_GPC['num'];
					$data = array(
						'uniacid'     =>$uniacid,
						'title'      =>$title,
						'logo'     =>$logo,
						'backdrop'    =>$backdrop,
						'originator'    =>$originator,
						'num'    =>$num
					);
					if(empty($id)){
						pdo_insert('luck_draw', $data);//添加数据
						$id = pdo_insertid();
						//message('数据添加成功！', $this->createWebUrl('activity', array('op' => 'display')), 'success');
						message('保存成功,即将进入下一步',$this->createWebUrl('activity',array('id'=>$id,'op'=>'post','step'=>'2')),'success');
					}else{
						pdo_update('luck_draw', $data, array('id' => $id));
						//message('数据更新成功！', $this->createWebUrl('activity', array('op' => 'display')), 'success');
						message('保存成功,即将进入下一步',$this->createWebUrl('activity',array('id'=>$id,'op'=>'post','step'=>'2')),'success');
					}
				}
			}elseif($step == 3) {
				if (checksubmit('display')) {
					if (!empty($_GPC['displayorder'])) {
						foreach ($_GPC['displayorder'] as $item_id => $displayorder) {
							pdo_update('luck_draw_item', array('displayorder' => $displayorder), array('id' => $item_id));
						}
						message('排序更新成功！', $this->createWebUrl('activity',array('id'=>$id,'op'=>'post','step'=>'2')), 'success');
					}
				}
				if (checksubmit('submit')) {
					$insert = array(
						'uniacid' => $_W['uniacid'],
						'draw_id' => intval($_GPC['id']),
						'displayorder' => intval($_GPC['displayorder']),
						'level' => $_GPC['level'],
						'limit_num' => intval($_GPC['limit_num'])
						);
					if (empty($item_id)) {
						pdo_insert('luck_draw_item', $insert);
					} else {
						pdo_update('luck_draw_item', $insert, array('id' => $item_id));
					}
					message('保存成功,继续添加',$this->createWebUrl('activity',array('id'=>$id,'op'=>'post','step'=>'2')),'success');
				}
				$items = pdo_fetchall("SELECT * FROM " . tablename('luck_draw_item') . " WHERE uniacid = '{$_W['uniacid']}' AND draw_id = '{$id}' ORDER BY id ASC");
				if (empty($items)) {
					message('您尚未添加活动奖项，请返回添加',$this->createWebUrl('activity',array('id'=>$id,'op'=>'post','step'=>'2')),'error');
				}
			}elseif ($step == 4) {
				if (checksubmit('finish')) {
					pdo_update('luck_draw', array('status' => 1), array('id' => $id));
					message('恭喜您，活动已经成功开始！',$this->createWebUrl('activity',array('op'=>'display')),'success');
				}else{
					message('活动保存成功！',$this->createWebUrl('activity',array('op'=>'display')),'success');
				}
			}
		}else if('del' == $op){//删除
			if(isset($_GPC['delete'])){
				$ids = implode(",",$_GPC['delete']);
				$sqls = "delete from  ".tablename('luck_draw')."  where id in(".$ids.")"; 
				pdo_query($sqls);
				message('删除成功！', referer(), 'success');
			}
			$id = intval($_GPC['id']);
			$row = pdo_fetch("SELECT id FROM ".tablename('luck_draw')." WHERE id = :id", array(':id' => $id));
			if (empty($row)) {
				//dump($_GPC);
				message('抱歉，数据不存在或是已经被删除！', $this->createWebUrl('activity', array('op' => 'display')), 'error');
			}
			pdo_delete('luck_draw', array('id' => $id));
			message('删除成功！', referer(), 'success');
		}else if('display' == $op){//显示
			$pindex = max(1, intval($_GPC['page']));
			$psize = 10;//每页显示
			$sql = "SELECT * FROM ".tablename('luck_draw') ." WHERE uniacid=:uniacid ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize;
			$params = array(':uniacid'=>$uniacid);
			$lists = pdo_fetchall($sql, $params);//分页
			$list = array();
			foreach ($lists as $k) {
				$rows = array( 
					'id'      => $k['id'],
					'title'    => $k['title'],
					'status' => $k['status']
				);
				array_push($list, $rows);
			}
			$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('luck_draw')." WHERE uniacid=:uniacid",array(':uniacid'=>$uniacid));
			$pager = pagination($total, $pindex, $psize);
		}
		include $this->template('activity');
	}

	public static function str_murl($url) {
		global $_W;
		return $_W['siteroot'] . 'app' . str_replace('./', '/', $url);
	}
	
	public function doMobileLottery(){
		global $_W,$_GPC;
		$uniacid =$_W['uniacid'];
		$id = intval($_GPC['id']);
		
		$activity = pdo_fetch("SELECT * FROM ".tablename('luck_draw')." where uniacid=:uniacid AND id=:id",array(':uniacid'=>$uniacid, ':id'=>$id));

		$items = pdo_fetchall("SELECT * FROM " . tablename('luck_draw_item') . " WHERE uniacid = '{$uniacid}' AND draw_id = '{$id}' ORDER BY displayorder ASC");
		
		include $this->template('index');
	}
	
	public function doMobileGetProinfo() {
		global $_W,$_GPC;
		$pro_id = intval($_GPC['pro_id']);
		$pro_content =  pdo_fetchcolumn("SELECT pro_content FROM ".tablename('love_chou_project')." WHERE pro_id=:pro_id",array(':pro_id'=>$pro_id));
		$proInfo['info']['desc'] = html_entity_decode($pro_content);
		echo json_encode($proInfo);
		exit;
	}
}